﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ReTryTrigger : MonoBehaviour
{
    public Vector2 RetryPosition = new Vector2(0, 0);
    public float BufferTime = 1f;
    BallUserController player;
    Rigidbody2D playerRgd;
    Coroutine coroutine;
    private void Awake()
    {
        player = GameObject.FindGameObjectWithTag(Constants.Tag_Player).GetComponent<BallUserController>();
        playerRgd = player.GetComponent<Rigidbody2D>();
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (coroutine == null && player.transform == collision.transform)
        {
            coroutine = StartCoroutine(DisablePlayerInput());
        }
    }

    IEnumerator DisablePlayerInput()
    {
        player.EnableInput = false;
        playerRgd.Sleep();
        yield return new WaitForSeconds(BufferTime);
        player.transform.position = RetryPosition;    
        player.EnableInput = true;
        coroutine = null;
    }
}
